package com.pactera.jep.service.sys.web.controller;

import com.pactera.jep.autoconfigure.web.controller.BaseRestController;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.model.RoleStd;
import com.pactera.jep.sys.service.RoleStdService;
import com.pactera.jep.web.annotation.PermitRequired;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.code.WebHeaderCode;
import com.pactera.jep.web.entity.NameValuePair;
import com.pactera.jep.web.util.NVUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 标准角色类型
 * @author yangyao<yao.yang@pactera.com>
 * @time 2018年06月6日 10:42
 * @company 文思海辉技术有限公司
 */
@Api(value = "标准角色类型")
@RestController
@RequestMapping("/roleStd")
public class RoleStdRestController extends BaseRestController<RoleStd> {

    Logger logger = LoggerFactory.getLogger(RoleStdRestController.class);

    @Autowired
    RoleStdService roleStdService;

    @Override
    @PermitRequired({"attachTypeName", "attachPath"})
    public Map<String, Object> query(String token, Page<RoleStd> page, Model model, HttpServletRequest request) {
        return super.query(token, page, model, request);
    }

    /**
     * 为列表页面做数据准备
     */
    @Override
    protected void prepareQuery(@ApiIgnore @RequestHeader(WebHeaderCode.X_USER_TOKEN) String token, Page<RoleStd> page, List<Filter> filters, List<Order> orders, HttpServletRequest request) {
        roleStdService.query(page, filters, orders);
    }

    /**
     * 保存
     * @param roleStd
     * @return
     */
    @ApiOperation(value = "保存一条数据")
    @PostMapping(value = "/save")
    public Map<String, Object> save(@RequestBody RoleStd roleStd) {

        Map<String, Object> tips = new HashMap<String, Object>();
        Boolean isExist = roleStdService.exists(roleStd.getRoleStdCode());
        try
        {
            if(isExist)
            {
                roleStdService.update(roleStd);
            }
            else
            {
                roleStdService.insert(roleStd);
            }
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
        }
        catch (ServiceException e)
        {
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
        }
        return tips;
    }

    /**
     * 删除
     * @param roleStdCode
     * @return
     */
    @ApiOperation(value = "根据编码删除一条数据")
    @PostMapping(value = "/remove/{roleStdCode}")
    public Map<String, Object> remove(@PathVariable String roleStdCode){

        Map<String, Object> tips = new HashMap<String, Object>();
        try
        {
            this.roleStdService.deleteByPrimaryKey(roleStdCode);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
        }
        catch (ServiceException e)
        {
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_DELETE+",请检查该角色编码是否被使用");
        }
        return tips;
    }

    /**
     * 验证主键是否已经存在数据库中
     * @param roleStdCode 角色类型主键
     * @return 返回验证结果true|false
     */
    @ApiOperation(value = "验证提交的编码是否已经存在数据库中")
    @GetMapping(value = "/check", produces = "application/json;charset=UTF-8")
    public Map<String, Object> check(String roleStdCode){

        Map<String, Object> tips = new HashMap<String, Object>();
        Boolean isExist = roleStdService.exists(roleStdCode);
        tips.put("valid", !isExist);

        return tips;
    }

    /**
     * 以特殊格式获取所有角色类型
     * @return json格式返回
     */
    @ApiOperation(value = "查询所有任务类型，并以 key-value 格式返回")
    @GetMapping(value = "/queryAll")
    public List<NameValuePair> queryAll(){
        List<RoleStd> result = roleStdService.selectAll();
        return NVUtils.convert(result, result.size(), "roleStdCode", "roleStdName");
    }
}
